#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\quote.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

#define HANDLE
#define ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IQuote
	public ref class CQuote : 
            public System::MarshalByRefObject,
            public Cephei::QL::IQuote
	{
	protected: 
		boost::shared_ptr<QuantLib::Quote>* _ppQuote;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Quote>* _phQuote;
#endif
		Object^ _QuoteOwner;     // reference to object that manages the storage for this object
	internal:
        CQuote (boost::shared_ptr<QuantLib::Quote>& childNative, Object^ owner);
        CQuote (QuantLib::Quote& childNative, Object^ owner);
        CQuote (CQuote^ copy);
        CQuote (System::Type^ t);
#ifdef STRUCT
        CQuote (QuantLib::Quote childNative);
#endif       
#ifdef HANDLE
		CQuote (QuantLib::Handle<QuantLib::Quote>& childNative, Object^ owner);
		CQuote (QuantLib::Handle<QuantLib::Quote> childNative);
#endif
		virtual ~CQuote ();
		!CQuote ();

	internal:
		QuantLib::Quote& GetReference ();
		boost::shared_ptr<QuantLib::Quote>& GetShared ();
		QuantLib::Quote* GetPointer ();
        void SetQuote (boost::shared_ptr<QuantLib::Quote> native)
        {
            if (_ppQuote != NULL)
                delete _ppQuote;
            _ppQuote = new boost::shared_ptr<QuantLib::Quote> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Quote>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
        property Boolean IsValid 
        {
		    virtual Boolean get () ;
        }
        property Double Value 
        {
		    virtual Double get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IQuote Vector interface
	ref class CQuoteVector : public System::MarshalByRefObject,  
							    public INativeVector<IQuote^>,
							    public INativeVector<Generic::ICell<IQuote^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::Quote>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::Quote>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Quote>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CQuoteVector ();
#ifdef STRUCT
		CQuoteVector (std::vector<QuantLib::Quote>& vec, Object^ owner);
		CQuoteVector (std::vector<QuantLib::Quote> vec);
#endif       
		CQuoteVector (std::vector<boost::shared_ptr<QuantLib::Quote>>& vec, Object^ owner);
#ifdef HANDLE
		CQuoteVector (std::vector<QuantLib::Handle<QuantLib::Quote>>& vec, Object^ owner);
#endif
		virtual ~CQuoteVector ();
		!CQuoteVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IQuote^ default[int] 
		{
			virtual IQuote^ get (int index) = INativeVector<IQuote^>::default::get;
			virtual void set (int index, IQuote^ value) = INativeVector<IQuote^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Generic::ICell<Cephei::QL::IQuote^>^ CItem[int] 
		{
			virtual Generic::ICell<IQuote^>^ get (int index) = INativeVector<Generic::ICell<IQuote^>^>::default::get;
			virtual void set (int index, Generic::ICell<IQuote^>^ value) = INativeVector<Generic::ICell<IQuote^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::Quote>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::Quote>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Quote>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IQuote Matrix interface
	ref class CQuoteMatrix : public System::MarshalByRefObject,  
								public INativeMatrix<IQuote^>,
							    public INativeMatrix<Generic::ICell<IQuote^>^>
	{
	private:
#ifdef STRUCT
        std::vector<std::vector<QuantLib::Quote>>* _pVValue;
#endif       
		std::vector<std::vector<boost::shared_ptr<QuantLib::Quote>>>* _pVBoost;
#ifdef HANDLE
		std::vector<std::vector<QuantLib::Handle<QuantLib::Quote>>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner
	internal:
		CQuoteMatrix ();
#ifdef STRUCT        
		CQuoteMatrix (std::vector<std::vector<QuantLib::Quote>>& vec, Object^ owner);
		CQuoteMatrix (std::vector<std::vector<QuantLib::Quote>> vec);
#endif        
		CQuoteMatrix (std::vector<std::vector<boost::shared_ptr<QuantLib::Quote>>>& vec, Object^ owner);
#ifdef HANDLE
		CQuoteMatrix (std::vector<std::vector<QuantLib::Handle<QuantLib::Quote>>>& vec, Object^ owner);
#endif
		virtual ~CQuoteMatrix ();
		!CQuoteMatrix ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property Cephei::QL::IQuote^ default[int, int] 
		{
			virtual IQuote^ get (int x, int y) = INativeMatrix<IQuote^>::default::get;
			virtual void set (int x, int y, IQuote^ value) = INativeMatrix<IQuote^>::default::set;
		}

		property Generic::ICell<Cephei::QL::IQuote^>^ CItem[int, int] 
		{
			virtual Generic::ICell<IQuote^>^ get (int x, int y) = INativeMatrix<Generic::ICell<IQuote^>^>::default::get;
			virtual void set (int x, int y, Generic::ICell<IQuote^>^ value) = INativeMatrix<Generic::ICell<IQuote^>^>::default::set;
		}

	internal:
#ifdef STRUCT
        std::vector<std::vector<QuantLib::Quote>>& GetReference ();
#endif        
		std::vector<std::vector<boost::shared_ptr<QuantLib::Quote>>>& GetShared ();

#ifdef HANDLE
		std::vector<std::vector<QuantLib::Handle<QuantLib::Quote>>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	public:
		virtual INativeVector<IQuote^>^ CreateVector () = INativeMatrix<IQuote^>::CreateVector;
	public:
		virtual INativeVector<Generic::ICell<IQuote^>^>^ CreateCellVector () = INativeMatrix<Generic::ICell<IQuote^>^>::CreateVector;
	};

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CQuote_Factory : public System::MarshalByRefObject,  public IQuote_Factory
	{
	public:
        /* <summary> Create a hybrid Matrix of IQuote, with event notification of changes </summary> */
        virtual Cephei::IMatrix<IQuote^>^ CreateMatrix();
        /* <summary> Create a hybrid Matrix of ICell of IQuote, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IMatrix<Generic::ICell<IQuote^>^>^>^ CreateCellMatrix();
        virtual Cephei::IMatrix<IQuote^>^ CreateMatrix(System::Collections::Generic::IEnumerable<Cephei::IVector<IQuote^>^>^ source);
        virtual Generic::ICell<Cephei::IMatrix<Generic::ICell<IQuote^>^>^>^ CreateCellMatrix(System::Collections::Generic::IEnumerable<Cephei::IVector<Generic::ICell<IQuote^>^>^>^ source);
        /* <summary> Create a hybrid Vector of IQuote, with event notification of changes </summary> */
        virtual Cephei::IVector<IQuote^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IQuote, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IQuote^>^>^>^ CreateCellVector();
        virtual Cephei::IVector<IQuote^>^ CreateVector(System::Collections::Generic::IEnumerable<IQuote^>^ source);
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IQuote^>^>^>^ CreateCellVector(System::Collections::Generic::IEnumerable<Generic::ICell<IQuote^>^>^ source);
    };
   
/*Cephei*/ } /*QL */}
